﻿//using System;
//using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Controls.Primitives;
//using System.Windows.Input;
//using System.Windows.Media;

//namespace Radovici.Ctrls
//{
//    /// <summary>
//    /// Concept and (some) code derived from http://www.codeproject.com/KB/silverlight/slmodal.aspx.
//    /// </summary>
//    public static class ModalHelper
//    {
//        public static Popup Popup { get; set; }

//        public static void Show(this UIElement ctrl, string title)
//        {
//            Show(ctrl, title, null, null);
//        }

//        public static void Show(this UIElement ctrl, string title, EventHandler callback, UIElement anchor)
//        {
//            new ModalDialog(ctrl, title, callback, anchor).Show();
//        }

//        public class ModalDialog : UserControl
//        {
//            private EventHandler _callback;
//            private UIElement _anchorCtrl;            
//            private ModalViewBackground _contentCtrl;            
//            private Canvas _bgScreen;            
//            private Popup _popup = null;

//            public ModalDialog(UIElement ctrl, string title) : this(ctrl, title, null, null) { }

//            public ModalDialog(UIElement ctrl, string title, EventHandler callback, UIElement anchor)
//            {
//                _anchorCtrl = anchor;
//                _callback = callback;
//                _contentCtrl = new ModalViewBackground(ctrl, title);
//                _contentCtrl.Loaded += OnHostLoaded;
//                _contentCtrl.SizeChanged += new SizeChangedEventHandler(OnHostSizeChanged);
//                _contentCtrl.OkButton.Click += new RoutedEventHandler(delegate { Close(); });
//                _contentCtrl.CancelButton.Click += new RoutedEventHandler(delegate { Close(true); });
//                _contentCtrl.CloseButton.Click += new RoutedEventHandler(delegate { Close(true); });
//                _bgScreen = new Canvas();
//                _bgScreen.Background = new SolidColorBrush(Color.FromArgb(50, 0, 0, 0));
//                _bgScreen.Children.Add(_contentCtrl);
//                this.Content = _bgScreen;

//                Application.Current.Host.Content.Resized += OnHostResized;

//                this.Loaded += OnHostLoaded;
//            }

//            public void Show()
//            {
//                _popup = ModalHelper.Popup ?? new Popup();
//                _popup.Child = this;
//                _popup.IsOpen = true;
//            }

//            public void Hide()
//            {
//                if (_popup != null)
//                {
//                    _popup.IsOpen = false;
//                    _popup.Child = null;
//                    _popup = null;
//                }
//            }

//            public object InnerContent { get { return _contentCtrl.InnerContent; } }

//            protected override void OnKeyUp(KeyEventArgs e)
//            {
//                switch (e.Key)
//                {
//                    //case Key.Enter:
//                    case Key.Escape:
//                        e.Handled = true;
//                        this.Close(e.Key == Key.Escape);
//                        break;
//                }
//                base.OnKeyUp(e);
//            }

//            public void Close()
//            {
//                Close(false);
//            }

//            public void Close(bool canceled)
//            {
//                if (_callback != null && !canceled)
//                {
//                    _callback(this.InnerContent, EventArgs.Empty);
//                }
//                Hide();
//                Application.Current.Host.Content.Resized -= OnHostResized;
//                Loaded -= OnHostLoaded;
//                _contentCtrl.MouseLeftButtonDown -= OnMouseLeftButtonDown;
//                _contentCtrl.MouseLeftButtonUp -= OnMouseLeftButtonUp;
//                _contentCtrl.MouseMove -= OnMouseMove;
//                _bgScreen.Children.Clear();
//            }

//            void OnHostSizeChanged(object sender, SizeChangedEventArgs e)
//            {
//                OnHostResized(null, EventArgs.Empty);
//            }

//            void OnHostLoaded(object sender, RoutedEventArgs e)
//            {
//                OnHostResized(null, EventArgs.Empty);
//                _contentCtrl.MouseLeftButtonDown += OnMouseLeftButtonDown;
//                _contentCtrl.MouseLeftButtonUp += OnMouseLeftButtonUp;
//                _contentCtrl.MouseMove += OnMouseMove;
//                _contentCtrl.Focus();
//                //TODO: Focus on editable item.
//                //DependencyObject tmp = VisualTreeHelper.GetChild(_contentCtrl, 1); //FindControl
//            }

//            void OnHostResized(object sender, EventArgs e)
//            {
//                _bgScreen.Width = Application.Current.Host.Content.ActualWidth;
//                _bgScreen.Height = Application.Current.Host.Content.ActualHeight;
//                ResizeContent();
//            }

//            private void ResizeContent()
//            {
//                if (_anchorCtrl != null)
//                {
//                    GeneralTransform gt = _anchorCtrl.TransformToVisual(Application.Current.RootVisual as UIElement);
//                    Point offset = gt.Transform(new Point(0, 0));                    
//                    double controlLeft = offset.X;
//                    double controlTop = offset.Y;
//                    if (_contentCtrl != null && !double.IsNaN(_contentCtrl.ActualWidth))
//                        _contentCtrl.SetValue(Canvas.LeftProperty, controlLeft);
//                    if (_contentCtrl != null && !double.IsNaN(_contentCtrl.ActualHeight))
//                        _contentCtrl.SetValue(Canvas.TopProperty, controlTop);
//                }
//                else
//                {
//                    if (_contentCtrl != null && !double.IsNaN(_contentCtrl.ActualWidth) && !double.IsNaN(_bgScreen.Width))
//                        _contentCtrl.SetValue(Canvas.LeftProperty, (_bgScreen.Width - _contentCtrl.ActualWidth) / 2);
//                    if (_contentCtrl != null && !double.IsNaN(_contentCtrl.ActualHeight) && !double.IsNaN(_bgScreen.Height))
//                        _contentCtrl.SetValue(Canvas.TopProperty, (_bgScreen.Height - _contentCtrl.ActualHeight) / 2);
//                }
//                if ((double)_contentCtrl.GetValue(Canvas.LeftProperty) + _contentCtrl.ActualWidth > _bgScreen.Width)
//                {
//                    _contentCtrl.SetValue(Canvas.LeftProperty, _bgScreen.Width - _contentCtrl.ActualWidth);
//                }
//                if ((double)_contentCtrl.GetValue(Canvas.TopProperty) + _contentCtrl.ActualHeight > _bgScreen.Height)
//                {
//                    _contentCtrl.SetValue(Canvas.TopProperty, _bgScreen.Height - _contentCtrl.ActualHeight);
//                }
//            }

//            #region Dragging code

//            bool _mouseCaptured = false;
//            Point _lastMousePos = new Point();

//            void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
//            {
//                _lastMousePos = e.GetPosition(null);
//                _mouseCaptured = _contentCtrl.CaptureMouse();                
//            }

//            void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
//            {
//                _mouseCaptured = false;
//                _contentCtrl.ReleaseMouseCapture();
//                _lastMousePos = new Point();                
//            }

//            void OnMouseMove(object sender, MouseEventArgs e)
//            {
//                if (_mouseCaptured)
//                {
//                    Point mousePos = e.GetPosition(null);
//                    double dblX = (double)_contentCtrl.GetValue(Canvas.LeftProperty) + mousePos.X - _lastMousePos.X;
//                    double dblY = (double)_contentCtrl.GetValue(Canvas.TopProperty) + mousePos.Y - _lastMousePos.Y;
//                    _contentCtrl.SetValue(Canvas.LeftProperty, dblX);
//                    _contentCtrl.SetValue(Canvas.TopProperty, dblY);
//                    _lastMousePos = mousePos;
//                }
//            }
//            #endregion
//        }
//    }
//}
